﻿#include "lua_script.h"
#include <QString>
#include <fstream>
#include <QMap>
#include <QDebug>
#include <QtConcurrent/QtConcurrent>

Lua_script::Lua_script(QObject *parent):QObject(parent)
{
    //prothread=new QThread();
    //SysSignal::instance()->moveToThread(prothread);
    connect(SysSignal::instance(),SIGNAL(sig_movej()),this,SLOT(slot_movej()));
    connect(SysSignal::instance(),SIGNAL(sig_moveL()),this,SLOT(slot_moveL()));
    connect(SysSignal::instance(),SIGNAL(sig_movep()),this,SLOT(slot_moveP()));
    connect(SysSignal::instance(),SIGNAL(sig_moveC()),this,SLOT(slot_moveC()));

    connect(this,&Lua_script::load_string,SysSignal::instance(),&SysSignal::load_string);
    connect(this,&Lua_script::executepro,SysSignal::instance(),&SysSignal::execute);
    //prothread->start();
}

Lua_script::~Lua_script()
{
}

void Lua_script::slot_movej()
{
    QVector<double> T = mtarget["MoveAbsJ"];
    QString v=QString::fromStdString(mvcc);
    QString z=QString::fromStdString(mzone);
    QString t=QString::fromStdString(mtool);
    QString w=QString::fromStdString(mwobj);
    QString l=QString::fromStdString(mload);
    qDebug()<<"Lua_script::slot_movej() "<<T<<v<<z<<t<<w<<l;
    emit sig_movej(T,v,z,t,w,l);
}

void Lua_script::slot_moveL()
{
    QVector<double> T = mtarget["MoveL"];
    QString v=QString::fromStdString(mvcc);
    QString z=QString::fromStdString(mzone);
    QString t=QString::fromStdString(mtool);
    QString w=QString::fromStdString(mwobj);
    QString l=QString::fromStdString(mload);
    qDebug()<<"Lua_script::slot_movej() "<<T<<v<<z<<t<<w<<l;
    emit sig_movej(T,v,z,t,w,l);
}

void Lua_script::slot_moveP()
{
    QVector<double> T = mtarget["MoveP"];
    QString v=QString::fromStdString(mvcc);
    QString z=QString::fromStdString(mzone);
    QString t=QString::fromStdString(mtool);
    QString w=QString::fromStdString(mwobj);
    QString l=QString::fromStdString(mload);
    qDebug()<<"Lua_script::slot_movej() "<<T<<v<<z<<t<<w<<l;
    emit sig_movej(T,v,z,t,w,l);
}

void Lua_script::slot_moveC()
{
    QVector<double> T = mtarget["MoveC"];
    QString v=QString::fromStdString(mvcc);
    QString z=QString::fromStdString(mzone);
    QString t=QString::fromStdString(mtool);
    QString w=QString::fromStdString(mwobj);
    QString l=QString::fromStdString(mload);
    qDebug()<<"Lua_script::slot_movej() "<<T<<v<<z<<t<<w<<l;
    emit sig_movej(T,v,z,t,w,l);
}

int Lua_script::getProStatus()
{
    return (int)status;
}

void Lua_script::load_program(QString pro)
{
    qDebug()<<"Lua_script::load_program";
    _mpro=pro;
    //emit load_string();
    SysSignal::instance()->load_string();
}

void Lua_script::runprogram()
{
    SysSignal::instance()->execute();
    //emit executepro();
}
void Lua_script::nextrun()
{
    _Next=true;
}

void Lua_script::setvcc(string vc)
{
        mvcc=vc;
}

string Lua_script::getvcc()
{
    return mvcc;
}

void Lua_script::setzone(string zone)
{
     mzone=zone;
}

string Lua_script::getzone()
{
    return mzone;
}

void Lua_script::settool(string tool)
{
        mtool=tool;
}

string Lua_script::gettool()
{
    return mtool;
}

void Lua_script::setwobj(string wobj)
{
        mwobj=wobj;
}

string Lua_script::getwobj()
{
    return mwobj;
}

void Lua_script::setload(string load)
{
    mload=load;
}

string Lua_script::getload()
{
    return mload;
}
